/**
 * MagicGrid KeyEvent Plugin v1.0
 * author: Mac_J@boarsoft.com
 * need: grid.js, core.js
 */
mac.grid_keyEvt = function(self, cfg) {
	cfg = $.extend(self.config, {
		keyEvt : $.extend({
			tabIndex : 1
		}, cfg)
	});
	var mm = self.children('.main');
	var hd = mm.children('.head');
	var bd = mm.children('.body');
	var tt = bd.children('.tt');
	var acfg = cfg.keyEvt;
	tt.keyup(function(e) {
		var tg = $(e.target), td = tg.closest('.td');
		var tr = td.parent(), v = ':visible';
		switch (e.keyCode) {
		case 13: // RETURN
		case 9: // TAB
			var sti = acfg.tabIndex, d = e.shiftKey? -1:1;
			var ti = tg.attr('tabIndex');
			ti = ti ? ti + d : sti;
			var a = tr.find('[tabIndex=' + ti + ']');
			while (a.length && !a.is(':visible')){
				ti += d;
				a = tr.find('[tabIndex=' + ti + ']');
			}
			if (a.length && a.is(':visible')) {
				a.focus();
			} else {
				var s = '.tr' + v;
				var t = (d<0? tr.prevAll(s):tr.nextAll(s)).first();
				if (t.length) {
					t.click();
					t.find(v + '[tabIndex=' + sti + ']').focus();
				}
			}
			return false;
		}
		return true;
	}).keydown(function(e) {
		if (e.keyCode == 9)
			return false;
	});
}